package admin

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	apiAdmin "miaoyu/api/backend/admin"
	"miaoyu/internal/service"
)

type mainCtl struct {
}

var Main = new(mainCtl)

func (c *mainCtl) Index(ctx context.Context, req *apiAdmin.IndexReq) (res *apiAdmin.IndexRes, err error) {

	//enforcer := service.Access()
	//
	//// add a permission node for role
	//enforcer.AddPolicy("role_1", "node_1")
	//fmt.Println("add policy")
	//// batch add permission nodes for roles
	//enforcer.AddPolicies([][]string{
	//	{"role_2", "node_3"},
	//	{"role_2", "node_4"},
	//	{"role_2", "node_2"},
	//	{"role_3", "node_3"},
	//})
	//
	//// add a role for user
	//enforcer.AddGroupingPolicy("user_1", "role_1")
	//
	//// batch add roles for users
	//enforcer.AddGroupingPolicies([][]string{
	//	{"user_2", "role_2"},
	//	{"user_3", "role_3"},
	//})
	//miwen, _ := function.RsaEncrypt([]byte("加密秘闻"))
	//fmt.Println(gconv.String(miwen))
	//miwenTxt, _ := function.RsaDecrypt([]byte("hmnvyi8vc1Jc+jVK+5EKMmFClXa4U1D+fhnGI3uCTajEoaK6KgNhzapR+f7iVWrK8KsrjBafUr0mWudb0sI/VQ=="))

	//fmt.Println(gconv.String(miwenTxt))

	//info, _ := utility.AesEncrypt("1")
	//fmt.Println("加密", info)
	//decodeInfo, _ := utility.AesDecrypt(gconv.String(info))
	//fmt.Println("解密", decodeInfo)

	service.TplResp().BuildTplWithLayout(g.RequestFromCtx(ctx), "main.html").WriteWithExit()
	return
}

func (c *mainCtl) Dashboard(ctx context.Context, req *apiAdmin.DashboardReq) (res *apiAdmin.DashboardRes, err error) {
	//userId := gconv.Int(service.Auth().GetIdentity(ctx))

	resp := service.TplResp().BuildTplWithLayout(g.RequestFromCtx(ctx), "backend/public/layout.html")

	//resp.BindFunc("AesEncrypt", utility.AesEncrypt)

	resp.WriteWithExit(g.MapStrAny{
		"mainTpl":  "/backend/main/dashboard.html",
		"siteName": "控制台",
	})
	return
}
func (c *mainCtl) DoMainInit(ctx context.Context, req *apiAdmin.InitDoReq) (res *apiAdmin.InitDoRes, err error) {
	initData, initErr := service.Main().InitInfo(ctx)
	if initErr != nil {
		g.Log().Print(ctx, "初始化信息接口失败")
		g.Log().Print(ctx, initData)
	}
	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))
	commonJson.SetSuccessJsonExit("ok", initData)
	return
}
